package com.interrobang.gui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.interrobang.anime.Anime;
import com.interrobang.anime.AnimeList;

public class AddAnimePanel extends JPanel 
{
	/**
	 * 
	 */
	private static final long serialVersionUID = -1086693775432059757L;
	JButton create;
	JTextField title;
	JTextField episodes;
	JButton returnToMenu;
	MainPanel panel;
	AnimeList list;
	private final String FILENAME = "Data/data.haruhi";
	
	public AddAnimePanel(MainPanel panel)
	{
		this.panel = panel;
		FileInputStream fis = null;
		ObjectInputStream in = null;
		try
		{
			fis = new FileInputStream(FILENAME);
			in = new ObjectInputStream(fis);
			list = (AnimeList)in.readObject();
			in.close();
		}catch(IOException e)
		{
			list = new AnimeList();
		}
		catch(ClassNotFoundException e)
		{
			//do something here
		}
		ButtonListener listener = new ButtonListener();
		title = new JTextField(15);		
		episodes = new JTextField(8);
		create = new JButton("Add Anime");
		returnToMenu = new JButton("Back to Main menu");
		returnToMenu.addActionListener(listener);
		create.addActionListener(listener);
		setPreferredSize(new Dimension(250, 140));
		JLabel titleLabel = new JLabel("Title: ");
		add(titleLabel);
		add(title);
		JLabel episodeLabel = new JLabel("Episodes: ");
		add(episodeLabel);
		add(episodes);
		add(create);
		add(returnToMenu);
	}
	
	public void update()
	{
		FileInputStream fis = null;
		ObjectInputStream in = null;
		try
		{
			fis = new FileInputStream(FILENAME);
			in = new ObjectInputStream(fis);
			list = (AnimeList)in.readObject();
			in.close();
		}catch(IOException e)
		{
			list = new AnimeList();
		}
		catch(ClassNotFoundException e)
		{
			//do something here
		}
	}
	
	private class ButtonListener implements ActionListener
	{

		@Override
		public void actionPerformed(ActionEvent event) 
		{
			if(event.getSource() == returnToMenu)
			{
				panel.setAddActive(false);
				panel.setMenuActive(true);
				panel.update();
			}
			if(event.getSource() == create)
			{
				String name = title.getText();
				int number = 0;
				try{
				number = Integer.parseInt(episodes.getText());
				}catch(NumberFormatException e){JOptionPane.showMessageDialog(panel, "Animes must have atleast one episode!"); number = -1;return;}
				if(number == 0)
				{
					JOptionPane.showMessageDialog(panel, "Animes must have atleast one episode!");
					return;
				}
				if(name.length() == 0)
				{
					JOptionPane.showMessageDialog(panel, "Anime titles must contain atleast 1 character or number!");
				}
				else
				{
					Anime anime = new Anime(name, number);
					if(list.contains(anime))
					{
						JOptionPane.showMessageDialog(panel, "The database already contains an anime by that name!");
						return;
					}
					list.add(anime);
					FileOutputStream fos = null;
					ObjectOutputStream out = null;
					try
					{
						fos = new FileOutputStream(FILENAME);
						out = new ObjectOutputStream(fos);
						out.writeObject(list);
						out.close();
						JOptionPane.showMessageDialog(panel, name + " has successfully been added!");
					}catch(IOException ex)
					    {
						       System.out.print("File not foud");
						}
				}
			}
		}
		
	}
}
